﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhongMachTu.OModel;

namespace PhongMachTu.OModel
{
    static class OSession
    {
        private static User _loggedInUser;
        private static DateTime _loginTime;

        static OSession()
        {
        }

        public static DateTime LoginTime
        {
            get { return OSession._loginTime; }
            set { OSession._loginTime = value; }
        }

        public static User LoggedInUser
        {
            get { return OSession._loggedInUser; }
            set { OSession._loggedInUser = value; }
        }

        public static void logout()
        {
            _loggedInUser = null;
        }

        public static bool isLoggedIn()
        {
            if (_loggedInUser == null)
            {
                return false;
            }
            else if (!isSessionValid())
            {
                return false;
            }
            else
            {
                LoginTime = DateTime.Now;
                return true;
            }
        }
        //Check session validity by time
        private static bool isSessionValid()
        {
            DateTime now = DateTime.Now;
            TimeSpan diff = now.Subtract(_loginTime);
            return diff.Minutes <= 1;
        }
    }
}
